package com.daichi.yygh.msm.service.impl;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.daichi.yygh.msm.service.MsmService;
import com.daichi.yygh.msm.utils.MsmUtils;
import com.daichi.yygh.vo.msm.MsmVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(String phone, String code) {
        // 校验存在
        if (!StringUtils.hasLength(phone) || !StringUtils.hasLength(code)) return false;

        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(MsmUtils.SERVER_IP, MsmUtils.SERVER_PORT);
        sdk.setAccount(MsmUtils.ACCOUNT_SID, MsmUtils.ACCOUNT_TOKEN);
        sdk.setAppId(MsmUtils.APP_ID);
        sdk.setBodyType(BodyType.Type_JSON);

        // 传入数据 参数1：验证码  参数2：过期时间
        String[] data = {code, "3"};

        // 发送短信，模板为1，短信验证码测试
        Map<String, Object> result = sdk.sendTemplateSMS(phone, "1", data);

        System.out.println("发送短信结果=============>" + "statusCode:" + result.get("statusCode") + "   statusMsg:" + result.get("statusMsg"));

        return MsmUtils.SUCCESS_CODE.equals(result.get("statusCode"));

    }

    @Override
    public boolean send(MsmVo msmVo) {
        // 这里就简单写一个特例，如果有多种短信应该发送这边搞个策略模式
        // 容联云免费测试只能发验证码短信，这段代码纯属乱写
        if (msmVo != null
                && StringUtils.hasLength(msmVo.getPhone())
                && StringUtils.hasLength(msmVo.getTemplateCode())
                && msmVo.getParam() != null) {
            Map<String, Object> param = msmVo.getParam();

            CCPRestSmsSDK sdk = new CCPRestSmsSDK();
            sdk.init(MsmUtils.SERVER_IP, MsmUtils.SERVER_PORT);
            sdk.setAccount(MsmUtils.ACCOUNT_SID, MsmUtils.ACCOUNT_TOKEN);
            sdk.setAppId(MsmUtils.APP_ID);
            sdk.setBodyType(BodyType.Type_JSON);

            // 传入数据
            String[] data = {
                    (String) param.get("title"),
                    (String) param.get("amount"),
                    (String) param.get("reserveDate"),
                    (String) param.get("name"),
                    (String) param.get("quitTime"),
            };

            // 发送短信
            Map<String, Object> result = sdk.sendTemplateSMS(msmVo.getPhone(), msmVo.getTemplateCode(), data);

            return MsmUtils.SUCCESS_CODE.equals(result.get("statusCode"));
        }
        return false;
    }

}
